package com.kidfruit.service;


import com.kidfruit.dao.TUserMapper;
import com.kidfruit.model.TUser;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userService")
public class UserService implements IUserService {

    @Autowired
    private TUserMapper tUserMapper;

    @Override
    public TUser getUserById(int userId) {
        // TODO Auto-generated method stub

        return this.tUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public List<TUser> getAllUsers() {
        Logger logger = Logger.getLogger(UserService.class);
        logger.info("测试");
        Logger logger2 = Logger.getLogger("testLogger");
        logger2.info("测试2");

        return tUserMapper.selectByExample(null);
    }

    @Override
    @Transactional
    public int addUser(TUser user) {
        return tUserMapper.insert(user);
    }

    @Override
    @Transactional
    public int deleteUserById(int userId) {
        return tUserMapper.deleteByPrimaryKey(userId);
    }

    @Override
    @Transactional
    public int deleteUsers(List<Integer> userIdList) {
        int rows = 0;
        for (int userId : userIdList) {
            rows += deleteUserById(userId);
        }
        return rows;
    }

    @Override
    @Transactional
    public int updateUser(TUser user) {
        return tUserMapper.updateByPrimaryKey(user);
    }

}